package com.xm.xmapp;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.core.QRCodeView.Delegate;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

import com.xm.xmapp.app.CodeConstant;
import com.xm.xmapp.base.BaseActivity;
import com.xm.xmapp.tools.Tools;

@SuppressWarnings("deprecation")
public class MipcaActivityCapture extends BaseActivity implements Delegate {

	/**
	 * 0是扫码租车1是扫码充电2是车辆问题反馈3是充电桩问题反馈
	 */
	private int flag;

	private LinearLayout bottom, open, root;
	private TextView saoma, bianhao, text, openText, startCharge;
	private ImageView car;
	private EditText number;
	private static final String TAG = TestScanActivity.class.getSimpleName();
	private QRCodeView mQRCodeView;
	private boolean isOpen;

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		flag = getIntent().getIntExtra("flag", 0);
		if (flag == 0 || flag == 2) {
			setContentView(R.layout.activity_capture);

		} else {
			setContentView(R.layout.activity_capture_order);
		}

		mQRCodeView = (ZBarView) findViewById(R.id.zbarview);
		mQRCodeView.setDelegate(this);
		isOpen = false;
		bottom = (LinearLayout) findViewById(R.id.bottom);

		root = (LinearLayout) findViewById(R.id.root);
		saoma = (TextView) findViewById(R.id.saoma);
		bianhao = (TextView) findViewById(R.id.bianhao);
		openText = (TextView) findViewById(R.id.openText);
		open = (LinearLayout) findViewById(R.id.open);
		text = (TextView) findViewById(R.id.text);
		car = (ImageView) findViewById(R.id.car);
		startCharge = (TextView) findViewById(R.id.startCharge);
		number = (EditText) findViewById(R.id.number);
		// 0是扫码租车1是扫码充电2是车辆问题反馈3是充电桩问题反馈

		switch (flag) {
		case 0:
			setMyTitle("扫码用车");

			text.setText("车辆正驾驶的挡风板上印有二维码，\n 手机扫码即可用车");
			bottom.setVisibility(View.GONE);
			car.setVisibility(View.VISIBLE);
			break;
		case 1:
			setMyTitle("扫码");

			text.setText("请扫桩或屏上的二维码，即可充电");
			bottom.setVisibility(View.VISIBLE);
			car.setVisibility(View.GONE);
			break;
		case 2:
			setMyTitle("扫码");

			text.setText("车辆正驾驶的挡风板上印有二维码，\n 手机扫码即可获取车牌号");
			bottom.setVisibility(View.GONE);
			car.setVisibility(View.VISIBLE);
			break;
		case 3:
			setMyTitle("扫码");

			text.setText("请扫桩或屏上的二维码即可");
			bottom.setVisibility(View.GONE);
			car.setVisibility(View.GONE);
			break;

		default:
			break;
		}

		OnClickListener listen = new OnClickListener() {

			@Override
			public void onClick(View v) {
				Drawable topDrawable1, topDrawable2;
				switch (v.getId()) {
				case R.id.open:
					isOpen = !isOpen;
					if (isOpen) {
						mQRCodeView.openFlashlight();
						openText.setText("关闭手电筒");
					} else {
						mQRCodeView.closeFlashlight();
						openText.setText("打开手电筒");
					}
					break;
				case R.id.bianhao:

					if (root.getVisibility() != View.INVISIBLE) {
						topDrawable1 = getResources().getDrawable(R.mipmap.scan_icon_scan_press);
						topDrawable1.setBounds(0, 0, topDrawable1.getMinimumWidth(), topDrawable1.getMinimumHeight());
						bianhao.setCompoundDrawables(null, topDrawable1, null, null);
						bianhao.setTextColor(0xffF2cc1c);

						topDrawable2 = getResources().getDrawable(R.mipmap.scan_icon_number_normal);
						topDrawable2.setBounds(0, 0, topDrawable2.getMinimumWidth(), topDrawable2.getMinimumHeight());
						saoma.setCompoundDrawables(null, topDrawable2, null, null);
						saoma.setTextColor(0xffffffff);

						findViewById(R.id.top).setVisibility(View.VISIBLE);
						root.setVisibility(View.INVISIBLE);
						setMyTitle("充电编号");
						mQRCodeView.stopSpotAndHiddenRect();
					}
					break;
				case R.id.saoma:
					if (root.getVisibility() != View.VISIBLE) {
						topDrawable1 = getResources().getDrawable(R.mipmap.scan_icon_scan_normal);
						topDrawable1.setBounds(0, 0, topDrawable1.getMinimumWidth(), topDrawable1.getMinimumHeight());
						bianhao.setCompoundDrawables(null, topDrawable1, null, null);
						bianhao.setTextColor(0xffffffff);

						topDrawable2 = getResources().getDrawable(R.mipmap.scan_icon_number_press);
						topDrawable2.setBounds(0, 0, topDrawable2.getMinimumWidth(), topDrawable2.getMinimumHeight());
						saoma.setCompoundDrawables(null, topDrawable2, null, null);
						saoma.setTextColor(0xffF2cc1c);

						findViewById(R.id.top).setVisibility(View.INVISIBLE);
						root.setVisibility(View.VISIBLE);
						setMyTitle("扫码");
						mQRCodeView.startSpotAndShowRect();
					}
					break;
				case R.id.startCharge:

					if (number.getText().toString().equals("")) {
						Tools.showMessage("请输入编号");
					} else {
						Intent resultIntent = getIntent();
						Bundle bundle = new Bundle();
						bundle.putString("result", CodeConstant.HTML + "y=000#" + number.getText().toString() + "&t=2");
						bundle.putInt("flag", flag);
						// bundle.putParcelable("bitmap", bitmap);
						resultIntent.putExtras(bundle);

						// Tools.showMessage(resultString);
						setResult(RESULT_OK, resultIntent);
						finish();
					}

					break;

				default:
					break;
				}

			}
		};
		startCharge.setOnClickListener(listen);
		bianhao.setOnClickListener(listen);
		open.setOnClickListener(listen);
		saoma.setOnClickListener(listen);

	}

	@Override
	public void initView() {

	}

	@Override
	public void initData() {

	}

	@Override
	public void initEvent() {

	}

	@Override
	protected void onStart() {
		super.onStart();
		mQRCodeView.startCamera();
		mQRCodeView.showScanRect();
		mQRCodeView.startSpot();
	}

	@Override
	protected void onStop() {
		mQRCodeView.stopCamera();
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		mQRCodeView.onDestroy();
		super.onDestroy();
	}

	private void vibrate() {
		Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		vibrator.vibrate(200);
	}

	@Override
	public void onScanQRCodeSuccess(String result) {

		vibrate();
		Intent resultIntent = getIntent();
		Bundle bundle = new Bundle();
		bundle.putString("result", result);
		bundle.putInt("flag", flag);
		resultIntent.putExtras(bundle);
		setResult(RESULT_OK, resultIntent);
		finish();

	}

	@Override
	public void onScanQRCodeOpenCameraError() {
		Log.e(TAG, "打开相机出错");
	}

}